/**
 * class: SyncMain - Simulation Launcher
 * @author charmgil, xiang
 * CS2510 Project 1
 */

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import common.Config;
import common.Utils;
//import centralized.*;
//import distributed.*;
import distributed.Node;

public class SyncMain{
	public static void main( String[] args ) throws IOException{ 
		ArrayList<Thread> nodes = new ArrayList<Thread>();
		
		// interface
		System.out.println( "---------------------------------------------" );
		System.out.println( "[ CS2510 - Project 1: Clock Synchronization ]" );
		System.out.println( "---------------------------------------------" );
		System.out.println( " 1. Master/Slave Model");
		System.out.println( " 2. Distributed Model");
		System.out.println( " 3. Exit\n");
		System.out.print( "Please choose a model (1 or 2) >> ");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		try{
			
			String option = reader.readLine();
			System.out.println(option);
			
			// proc
			if(Integer.parseInt(option) == 1){
				// centralized
				centralized.Middleware network = new centralized.Middleware();
				network.start();
				
				for( int i = 0; i < Config.NUMBER_OF_NODES; i++ ){
					nodes.add( new centralized.Node( network ) );
					for( int j = 0; j < 5000000; j++ );
					nodes.get( i ).start();
				}
				
				Runnable input_box = new centralized.MsgInjector( network );
				Thread shell = new Thread( input_box );
				
				shell.start();
				
			}else if(Integer.parseInt(option) == 2){
				// distributed
				distributed.Middleware network = new distributed.Middleware();
				distributed.SystemTimer timer = new distributed.SystemTimer(network);
				network.start();
				timer.start();
				
				for( int i = 0; i < Config.NUMBER_OF_NODES; i++ ){
					nodes.add( new distributed.Node( network ) );
					for( int j = 0; j < 5000000; j++ );
					nodes.get( i ).start();
				}
				
				Runnable input_box = new distributed.MsgInjector(network);
				Thread shell = new Thread( input_box );
				
				shell.start();
			}else
				System.err.println("Exit without running simulations.");
				//System.err.println("You should type either 1 or 2.");
			
		}catch(IOException e){
			//System.err.println("You should type either 1 or 2.");	// -- commented by charmgil
			e.printStackTrace();
		}
		
	}//end-of-main()
}//end-of-class


